{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = 'all'\n",
    "import sys\n",
    "sys.path.append('../')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_deepseek import ChatDeepSeek\n",
    "\n",
    "llm = ChatDeepSeek(\n",
    "    model=\"deepseek-chat\",\n",
    "    temperature=0,\n",
    "    max_tokens=None,\n",
    "    timeout=None,\n",
    "    max_retries=2,\n",
    "    api_key=\"sk-e0ebc15c2d124d2cad19536757701fc6\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 同步流式传输"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|天空|的颜色|主要|取决于|大气|中的|散射|现象|以及|观察|的时间和|天气|条件|。|以下是|不同|情况下|天空|的颜色|：\n",
      "\n",
      "|1|.| **|晴朗|的|白天|**|：|天空|通常|呈现|**|蓝色|**|。|这是因为|太阳|光|中的|短|波长|蓝|光|在大|气|中被|散射|得|更多|，|而|人|眼|对|蓝|光|也更|敏感|。\n",
      "\n",
      "|2|.| **|日出|或|日落|**|：|天空|可能|呈现|**|橙色|、|红色|或|粉|红色|**|。|这是因为|太阳|光|在|低|角度|穿过|大气|层|时|，|蓝|光|被|散射|掉|，|而|红光|和|橙|光|更容易|穿透|大气|层|到达|地面|。\n",
      "\n",
      "|3|.| **|阴|天|或多|云|时|**|：|天空|通常|呈现|**|灰色|或|白色|**|，|因为|云|层|散射|了|所有|波|长的|光|，|使得|天空|看起来|暗淡|。\n",
      "\n",
      "|4|.| **|夜晚|**|：|天空|呈现|**|黑色|**|，|因为没有|太阳|光的|照射|，|只有|星光|和|月光|。|\n",
      "\n",
      "|5|.| **|特殊|天气|或|现象|**|：|例如|极|光|、|彩虹|或|沙|尘|暴|时|，|天空|可能|呈现|**|绿色|、|紫色|、|黄色|**|等|多彩|的颜色|。\n",
      "\n",
      "|所以|，|天空|的颜色|并不是|固定的|，|而是|随着|时间|、|天气|和|地理位置|的变化|而变化|。||"
     ]
    }
   ],
   "source": [
    "chunks = []\n",
    "for chunk in llm.stream(\"天空是什么颜色\"):\n",
    "    chunks.append(chunk)\n",
    "    print(chunk.content, end=\"|\", flush=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 异步流式传输"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|The| color| of| the| sky| typically| appears| blue| during| the| day| due| to| a| phenomenon| called| **|Ray|leigh| scattering|**.| When| sunlight| passes| through| the| Earth|'s| atmosphere|,| shorter| wavelengths| of| light| (|blue| and| violet|)| are| scattered| more| than| longer| wavelengths| (|red|,| orange|,| and| yellow|).| Our| eyes| are| more| sensitive| to| blue| light|,| so| the| sky| appears| blue|.\n",
      "\n",
      "|At| sunrise| or| sunset|,| the| sky| can| appear| red|,| orange|,| or| pink| because| the| sunlight| travels| through| more| of| the| atmosphere|,| scattering| the| shorter| blue| wavelengths| and| leaving| the| longer| red| and| orange| wavelengths| to| dominate|.|\n",
      "\n",
      "|At| night|,| the| sky| appears| dark| or| black| because| there| is| no| sunlight| to| scatter|.| However|,| stars|,| the| moon|,| and| other| celestial| objects| can| be| seen|.||"
     ]
    }
   ],
   "source": [
    "chunks = []\n",
    "async for chunk in llm.astream(\"what color is the sky?\"):\n",
    "    chunks.append(chunk)\n",
    "    print(chunk.content, end=\"|\", flush=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "每一个chunk都是可加的，因此只需将它们加在一起即可获得目前响应的状态！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessageChunk(content='The color', additional_kwargs={}, response_metadata={}, id='run-16f3f8ee-87d4-4710-b53d-a7b23bf5cce1')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chunks[0]+chunks[1]+chunks[2]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
